<?php

namespace App\Http\Resources;

use App\Services\TermHierarchyService;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use LaraPress\Sliver\LaraPressTerm;

class TermResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        $data = [
            'id' => $this->term_id,
            'name' => $this->name,
            'slug' => $this->slug,
            'description' => $this->description,
            'count' => $this->count,
            'parent' => $this->parent,
            'taxonomy' => $this->taxonomy,
            'url' => get_term_link($this->term_id),
            'cover' => LaraPressTerm::get_cover($this->term_id),
        ];

        // 注意：子分类的嵌套逻辑已移至TermHierarchyService中处理
        // 这里只返回基本的分类信息，children字段由服务类添加

        return $data;
    }
}
